package com.mabang.medata.server.authc;

import javax.servlet.http.HttpServletRequest;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.staxutils.W3CDOMStreamWriter;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

@Component
public class ErrorHandlerInterceptor extends AbstractSoapInterceptor{

	public ErrorHandlerInterceptor() {  
        super(Phase.MARSHAL);  
    }  
	
	public ErrorHandlerInterceptor(String p) {
		super(p);
	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		// 错误原因  
        Fault fault = (Fault) message.getContent(Exception.class);  
        // 错误信息  
        String errorMessage = null;  
        Throwable cause = null;  
        if (fault != null) {  
            errorMessage = fault.getMessage();  
            cause = fault.getCause();  
        }  
  
        Exchange exchange = message.getExchange();  
        // wsdl描述  
        String servicePath = null;  
        // url与uri  
        String url = null;  
        String uri = null;  
        // 客户端ip  
        String clientIp = null;  
        // 用户名  
        String username = null;  
        // 密码  
        String psw = null;  
        // 服务中的方法  
        String methodName = null;  
        // 参数名  
        Object[] paramNames = null;  
        // 参数值  
        Object[] paramValues = null;  
  
        if (exchange != null) {  
            Object object = exchange.get("javax.xml.ws.wsdl.description");  
            if (object != null) {  
                servicePath = object.toString();  
            }  
  
            Message inMessage = exchange.getInMessage();  
            if (inMessage != null) {  
                HttpServletRequest req = (HttpServletRequest) inMessage  
                        .get("HTTP.REQUEST");  
//                clientIp = getIpAddr(req);  
                url = (String) inMessage.get("org.apache.cxf.request.url");  
                uri = (String) inMessage.get("org.apache.cxf.request.uri");  
            }  
  
            W3CDOMStreamWriter w3CDOMStreamWriter = (W3CDOMStreamWriter) inMessage  
                    .get(W3CDOMStreamWriter.class.getName());  
            if (w3CDOMStreamWriter != null) {  
                Document document = w3CDOMStreamWriter.getDocument();  
                if (document != null) {  
                    NodeList usernames = document  
                            .getElementsByTagName("wsse:Username");  
                    NodeList psws = document  
                            .getElementsByTagName("wsse:Password");  
  
                    if (usernames != null && usernames.getLength() == 1) {  
                        username = usernames.item(0).getTextContent();  
                    }  
                    if (psws != null && psws.getLength() == 1) {  
                        psw = psws.item(0).getTextContent();  
                    }  
  
                    NodeList body = document.getElementsByTagName("soap:Body");  
                    Node method = body.item(0).getFirstChild();  
                    if (method != null) {  
                        methodName = method.getNodeName();  
                        String[] methods = methodName.split(":");  
                        if (methods != null && methods.length == 2) {  
                            methodName = methods[1];  
                        }  
  
                        NodeList args = method.getChildNodes();  
//                        paramNames = getParam(args, true);  
//                        paramValues = getParam(args, false);  
                    }  
                }  
            }  
        }  
        System.out.println(cause);  
        System.out.println(errorMessage);  
        System.out.println(servicePath);  
        System.out.println(url);  
        System.out.println(uri);  
        System.out.println(clientIp);  
        System.out.println(username);  
        System.out.println(psw);  
        System.out.println(methodName);  
        System.out.println("---names--");  
//        System.out.print(getParam(paramNames) + "\t");  
        System.out.println("\n---values--");  
//        System.out.print(getParam(paramValues) + "\t");  
	}

}
